package com.alstudio.ui.module.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.XmppErrorCode;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.utils.formater.ALEmailFormater;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

public class EmailBind extends TitleBarActivity {

	private EditText et_account, et_new_pwd;
	private TextView bind_email_ready;
	private int emailbind;
	private LinearLayout bind_layout;
	private ScrollView unbind_layout;
	private String email = "aaaa@aa.com";
	private String email_pwd = "";
	private final int REGISTER_TIMEOUT = 10000; //
	private boolean isEmailPwdShow = false;

	// private int loginType = App.LOGIN_TYPE_MEACH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
		initData();

		// 注册到我的页面列表
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.bind_email);
		setCenterTextViewMessage(R.string.TxtEmailBind);
//		setRightButtonBackground(R.drawable.friend_title_icon_confirm);
		rightUseImageButton(true);
		setRightButtonImageSrc(R.drawable.friend_title_icon_confirm);

		setRightButtonClickListener(this);
	}

	private void initUI() {
		et_account = (EditText) findViewById(R.id.et_account);
		bind_email_ready = (TextView) findViewById(R.id.bind_email_ready);
		et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);

		// 已绑定显示
		bind_layout = (LinearLayout) findViewById(R.id.bind_layout);
		unbind_layout = (ScrollView) findViewById(R.id.unbind_layout);

		if (emailbind == 1) { // 绑定显示
			bind_layout.setVisibility(View.VISIBLE);
			unbind_layout.setVisibility(View.GONE);
			bind_email_ready.setText(ALLocalEnv.getLocalEnv().getMyProfile()
					.getEmail());
			setRightButtonBackground(0);
			setRightButtonImageSrc(0);
			setRightButtonClickListener(null);

		} else { // 未绑定
			// if (App.myAccount.getLoginType() != loginType) { // 第三方登陆
			// et_new_pwd.setVisibility(View.VISIBLE);
			// et_account.setBackgroundResource(R.drawable.setting_bg_1up);
			isEmailPwdShow = true;
			bind_layout.setVisibility(View.GONE);
			unbind_layout.setVisibility(View.VISIBLE);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		emailbind = getIntent().getIntExtra("emailbind", -1);
		initUI();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		super.onClick(v);

	}

	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.TitleBarActivity#invokeRightBtnClick()
	 */
	@Override
	protected void invokeRightBtnClick() {
		// TODO Auto-generated method stub
		super.invokeRightBtnClick();
		invokeBindEmail();
	}
	
	private void invokeBindEmail() {

		ALKeyBoardManager.dismissKeyBoard(this);

		if (et_account.getText().toString().trim().length() == 0) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtHintEnterEmail));
			et_account.requestFocus();
			return;
		}

		// 检测邮箱合法性
		if (!ALEmailFormater.isEmail(et_account.getText().toString().trim())) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtEmailFormatError));
			et_account.requestFocus();
			return;
		}

		if (!ALLocalEnv.isSendXmppDataEnable())
			return;
		ALUiXmppRequestCreator.postALUiXmppBindEmail(et_account.getText()
				.toString().trim(), ALLocalEnv.getLocalEnv().getMyAccount()
				.getPassWord());
		ALLocalEnv.getLocalEnv().getMyProfile()
				.setEmail(et_account.getText().toString().trim());
		// 未验证
		ALLocalEnv.getLocalEnv().getMyProfile().setEmailIsBind(1);

		showLoadingProgress();

	}

	/**
	 * 查看绑定的邮箱
	 */
	public void showEmailSendDetail() {
		Intent intent = new Intent();
		intent.setClass(this, EmailSendDetail.class);
		intent.putExtra("email", email);
		startActivity(intent);
		finish();
	}

	@Override
	public void handleBindEmailResult(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleBindEmailResult(data);
		dismissLoadingProgress();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			// 绑定请求成功
			showEmailSendDetail();
		} else {
			// 绑定请求失败
			String msg = "";
			switch (data.getResponseCode()) {
			case XmppErrorCode.XMPP_ERROR_801:
				msg = getString(R.string.TxtBindEmailFail801);
				break;
			case XmppErrorCode.XMPP_ERROR_802:
				msg = getString(R.string.TxtBindEmailFail802);
				break;
			default:
				msg = getString(R.string.TxtUnknowError);
				break;
			}
			MyToastUtil.getInstance().showCenterToastOnCenter(msg);
		}
	}

}
